package com.zzb.framework.modules.param.controller;

import com.zzb.framework.common.BasePage;
import com.zzb.framework.common.BaseResponse;
import com.zzb.framework.common.FrameworkException;
import com.zzb.framework.modules.log.annotation.SysLog;
import com.zzb.framework.modules.param.entity.SysParam;
import com.zzb.framework.modules.param.form.SysParamSearchForm;
import com.zzb.framework.modules.param.service.SysParamService;
import com.zzb.framework.modules.role.controller.AbstractController;
import com.zzb.framework.modules.role.entity.SysUserEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
* 系统配置表controller
*/
@Controller
@RequestMapping("/sys/param")
public class SysParamController extends AbstractController {
    private static final Logger logger = LoggerFactory.getLogger(SysParamController.class);
    @Autowired
    private SysParamService sysParamService;

    /**系统配置表管理首页*/
    @RequestMapping({"","/","/index"})
    public String index(){
        return "framework/sysParam/index";
    }

    /**系统配置表列表json*/
    @RequestMapping("list")
    @ResponseBody
    @RequiresPermissions("sys:param:list")
    public BasePage<SysParam> list(SysParamSearchForm sysParamSearchForm){
        return sysParamService.queryPage(sysParamSearchForm);
    }


    /**
     * 获取登录的用户信息
     */
    @GetMapping("/info/{id}")
    @ResponseBody
    @RequiresPermissions("sys:param:info")
    public BaseResponse<SysParam> info(@PathVariable("id") String id){
        SysParam sysParam = sysParamService.get(id);
        return BaseResponse.content(sysParam);
    }


    /**系统配置表表单页面*/
    @RequestMapping("form")
    public String form(String id, Model model){
        SysParam sysParam;
        if(StringUtils.isEmpty(id)){
            sysParam = new SysParam();
    } else{
        sysParam = sysParamService.get(id);
        if(sysParam == null) sysParam = new SysParam();
    }
        model.addAttribute("sysParam",sysParam);
        return "framework/sysParam/form";
    }

    /**系统配置表保存*/
    @RequestMapping("/save")
    @RequiresPermissions("sys:param:save")
    @ResponseBody
    @SysLog("保存配置")
    public BaseResponse<SysParam> save(@RequestBody SysParam sysParam ){
        try {
            SysUserEntity user = getUser();
            return sysParamService.saveSysParam(sysParam, user);
        } catch (FrameworkException e) {
            e.printStackTrace();
            return new BaseResponse<>(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**系统配置表删除*/
    @PostMapping("/delete")
    @RequiresPermissions("sys:param:delete")
    @ResponseBody
    @SysLog("删除配置")
    public BaseResponse delete( @RequestBody String[] id){
        try {
            return sysParamService.deleteSysParam(id,getUser());
        } catch (FrameworkException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }
}
